/*
 *    Copyright (c) 2018-2025, cloud All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: cloud
 */
package com.fuyao.cloud.personnel.secretasset.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fuyao.cloud.asset.entity.secretasset.SecretAsset;
import com.fuyao.cloud.asset.entity.secretasset.SecretAssetExportVO;
import com.fuyao.cloud.asset.entity.secretasset.SecretAssetImportVO;
import com.fuyao.cloud.common.core.constant.CommonConstants;
import com.fuyao.cloud.common.core.util.DictItemOption;
import com.fuyao.cloud.common.core.util.R;
import com.fuyao.cloud.common.data.resolver.DictItemResolver;
import com.fuyao.cloud.common.excel.vo.ErrorMessage;
import com.fuyao.cloud.personnel.secretasset.mapper.SecretAssetMapper;
import com.fuyao.cloud.personnel.secretasset.service.SecretAssetService;
import org.apache.commons.compress.utils.Lists;
import org.springframework.stereotype.Service;
import org.springframework.validation.BindingResult;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 涉密设备信息
 *
 * @author lyhills
 * @date 2023-04-12 13:59:45
 */
@Service
public class SecretAssetServiceImpl extends ServiceImpl<SecretAssetMapper, SecretAsset> implements SecretAssetService {

    @Override
    public List<SecretAssetExportVO> listExport(QueryWrapper<SecretAsset> query) {
        List<SecretAsset> secretAssetList = this.list(query);
        Map<String, List<DictItemOption>> map = DictItemResolver.getDictItemTypeList(Collections.singletonList("secretType")).getData();

        return secretAssetList.stream().map(secretAsset -> {
            SecretAssetExportVO exportDTO = new SecretAssetExportVO();
            BeanUtil.copyProperties(secretAsset, exportDTO);
            return exportDTO;
        }).collect(Collectors.toList());
    }

    @Override
    public R importSecretAsset(List<SecretAssetImportVO> excelVOList, BindingResult bindingResult) {
        // 通用校验获取失败的数据
        List<ErrorMessage> errorMessageList = (List<ErrorMessage>) bindingResult.getTarget();
        if (CollUtil.isEmpty(excelVOList)) {
            assert errorMessageList != null;
            errorMessageList.add(new ErrorMessage("无数据导入"));
            return R.failed(errorMessageList);
        }
        if(CollUtil.isNotEmpty(errorMessageList)){
            return R.failed(errorMessageList);
        }
        List<SecretAsset> secretAssetList = Lists.newArrayList();
        excelVOList.forEach(secretAssetImportVO -> {
            SecretAsset secretAsset = new SecretAsset();
            BeanUtil.copyProperties(secretAssetImportVO, secretAsset);
            // 处理可选项和日期数据
            /*String enableDate = secretAssetImportVO.getEnableDate();
            String dateRegex = "(\\d{4}-\\d{2}-\\d{2})";
            if (StrUtil.isNotEmpty(enableDate) && enableDate.matches(dateRegex)) {
                secretAsset.setEnableDate(LocalDateTimeUtil.parseDate(secretAssetImportVO.getEnableDate()));
            }else{
                secretAsset.setEnableDate(null);
            }
            String systemInstallDate = secretAssetImportVO.getSystemInstallDate();
            if (StrUtil.isNotEmpty(systemInstallDate) && systemInstallDate.matches(dateRegex)) {
                secretAsset.setSystemInstallDate(LocalDateTimeUtil.parseDate(secretAssetImportVO.getSystemInstallDate()));
            }else{
                secretAsset.setSystemInstallDate(null);
            }*/
            secretAssetList.add(secretAsset);
        });
        boolean saveBatch = super.saveBatch(secretAssetList);
        if (saveBatch) {
            return R.ok(CommonConstants.SUCCESS, "导入成功");
        }
        errorMessageList.add(new ErrorMessage("导入失败，请联系管理员。"));
        return R.failed(errorMessageList);
    }
}
